package com.jy.controller.filemanager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jy.common.ajax.AjaxRes;
import com.jy.common.mybatis.Page;
import com.jy.common.utils.base.Const;
import com.jy.common.utils.poi.ObjectExcelView;
import com.jy.controller.base.BaseController;
import com.jy.entity.filemanager.TSFile;
import com.jy.entity.filemanager.TSHitask;
import com.jy.entity.filemanager.TSTaskStatistic;
import com.jy.entity.filemanager.TSTaske;
import com.jy.service.filemanager.histask.HisTaskService;

@Controller
@RequestMapping("/histask")
public class HisTaskController extends BaseController<TSTaske> {

	@Autowired
	private HisTaskService service;

	/**
	 * @api {post} /histask/findBypage  任务分页列表查询
	 * 
	 * @apiName findBypage
	 * @apiGroup histask
	 * @apiParam {Object} Page<TSProduct> 分页对象
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct 任务对象
	 */
	@RequestMapping(value = "findBypage", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes index(Page<TSHitask> page, TSHitask o) {
		AjaxRes ar = getAjaxRes();
		try {
			Page<TSHitask> result = service.findByPage(o, page);
			if (result.getTotalRecord() > 1000) {
				result.setTotalRecord(1000);
				int totalPage = result.getTotalRecord() % result.getPageSize() == 0 ? result
						.getTotalRecord() / result.getPageSize()
						: result.getTotalRecord() / result.getPageSize() + 1;
				result.setTotalPage(totalPage);
			}
			Map<String, Object> p = new HashMap<String, Object>();
			p.put("list", result);
			ar.setSucceed(p);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.DATA_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /histask/findFileByHisTaskId  任务文件查询
	 * 
	 * @apiName findFileByHisTaskId
	 * @apiGroup histask
	 * @apiParam {Object} Page<TSProduct> 分页对象
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct 任务对象
	 */
	@RequestMapping(value = "findFileByHisTaskId", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes findFileByHisTaskId(TSHitask o) {
		AjaxRes ar = getAjaxRes();
		try {
			List<TSFile> result = service.findFileByHisTaskId(o);
			Map<String, Object> p = new HashMap<String, Object>();
			p.put("list", result);
			ar.setSucceed(p);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.DATA_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /histask/findTaskInfo  任务概况
	 * 
	 * @apiName findBypage
	 * @apiGroup find
	 * @apiParam {Object} Page<TSProduct> 分页对象
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 */
	@RequestMapping(value = "findTaskInfo", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes findTaskInfo(TSHitask o) {
		AjaxRes ar = getAjaxRes();
		try {
			Map<String, Object> resultMap = new HashMap<String, Object>();
			Integer allTaskNum = service.findTaskNum("");
			Integer weekTaskNum = service.findTaskNum("week");
			Integer allTaskFileNum = service.findTaskFileNum("");
			Integer weekTaskFileNum = service.findTaskFileNum("week");
			Integer allTaskProblemNum = service.findTaskProblemNum("");
			Integer weekTaskProblemNum = service.findTaskProblemNum("week");
			resultMap.put("allTaskNum", allTaskNum);
			resultMap.put("weekTaskNum", weekTaskNum);
			resultMap.put("allTaskFileNum", allTaskFileNum);
			resultMap.put("weekTaskFileNum", weekTaskFileNum);
			resultMap.put("allTaskProblemNum", allTaskProblemNum);
			resultMap.put("weekTaskProblemNum", weekTaskProblemNum);
			System.out.println(">>>" + resultMap);
			ar.setResMsg("获取成功");
			ar.setSucceed(resultMap);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setResMsg("获取失败");
			ar.setFailMsg(Const.DATA_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /histask/findTaskStaticInfo  任务统计查询
	 * 
	 * @apiName findTaskStaticInfo
	 * @apiGroup histask
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSTaskStatistic 查询对象 taskCode;
	 *           projectId;fileNum;numSour;
	 *           numUser;projectName;productName;userName;dateType
	 */
	@RequestMapping(value = "findTaskStaticInfo", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes findTaskStaticInfo(TSTaskStatistic o) {
		AjaxRes ar = getAjaxRes();
		try {
			List<TSTaskStatistic> resultMap = new ArrayList<TSTaskStatistic>();
			resultMap = service.findTaskStaticInfo(o);
			ar.setSucceed(resultMap);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setResMsg("获取失败");
			ar.setFailMsg(Const.DATA_FAIL);
		}
		return ar;
	}

	@RequestMapping(value = "exportXls", method = RequestMethod.POST)
	@ResponseBody
	public ModelAndView exportXls(TSTaskStatistic o) {
		ModelAndView mv = this.getModelAndView();
		try {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("fileName", "TaskLog");// 设置文件名
			List<String> titles = new ArrayList<String>();
			titles.add("任务编号"); // 1
			titles.add("所属项目"); // 2
			titles.add("产品源名称"); // 3
			titles.add("目标名称"); // 4
			titles.add("发送文件(个)"); // 5
			titles.add("源产品故障(次)"); // 6
			titles.add("目标故障(次)"); // 7
			model.put("titles", titles);// 设置标题们
			List<TSTaskStatistic> resultMap = new ArrayList<TSTaskStatistic>();
			resultMap = service.findTaskStaticInfo(o);
			Map<String, Object> p = new HashMap<String, Object>();
			List<Map<String, String>> varList = new ArrayList<Map<String, String>>();
			for (TSTaskStatistic t : resultMap) {
				Map<String, String> vpd = new HashMap<String, String>();
				vpd.put("var1", t.getTaskCode()); // 1
				vpd.put("var2", t.getProjectName()); // 2
				vpd.put("var3", t.getProductName()); // 3
				vpd.put("var4", t.getUserName()); // 4
				vpd.put("var5", t.getFileNum().toString()); // 5
				vpd.put("var6", t.getNumSour().toString()); // 6
				vpd.put("var7", t.getNumUser().toString()); // 7
				varList.add(vpd);
			}
			model.put("varList", varList);// 设置内容们
			ObjectExcelView erv = new ObjectExcelView(); // 执行excel操作
			mv = new ModelAndView(erv, model);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}
}
